#include "sliderbutton.h"

SliderButton::SliderButton(QWidget *parent) :
    QWidget (parent),
    m_button_status(false),
    m_circle_width(30),
    m_button_pos(0),
    m_move_distance(20),
    m_backcolor_on(Qt::red),
    m_backcolor_off(Qt::blue),
    m_circle_color(Qt::black)
{
//    setWindowFlags(Qt::FramelessWindowHint);
//    setAttribute(Qt::WA_TranslucentBackground);
    m_timer = new QTimer(this);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(slot_update()));
    this->setFixedSize(100,100); // 设置SliderButton窗口大小
}

SliderButton::~SliderButton()
{
}

void SliderButton::set_button_size(const int & width, const int &heigh)
{
    m_circle_width = heigh;
    m_move_distance = width;
}

void SliderButton::set_button_color(const QColor &off_color, const QColor &on_color, const QColor &button_color)
{
    m_backcolor_on = on_color;
    m_backcolor_off = off_color;
    m_circle_color = button_color;
}

void SliderButton::mousePressEvent(QMouseEvent *event)
{
    Q_UNUSED(event) // Q_UNUSED()函数在程序中没有实质性的作用，用来避免编译器警告
    if (false == m_button_status)
    {
        m_button_status = true;
        emit signal_button_on();
    }
    else
    {
        m_button_status = false;
        emit signal_button_off();
    }
    m_timer->start(1);
}

void SliderButton::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    QPainterPath path;

    // QPainter::Antialiasing //绘图抗锯齿
    // QPainter::TextAntialiasing //绘制的字体抗锯齿
    // setRenderHint(QPainter::Antialiasing, true);//反走样功能，调用反锯齿功能
    // 在QGraphicsItem中使用旋转，直线确实会出现锯齿，使用这个会不会好一些，待测试
    painter.setRenderHint(QPainter::Antialiasing, true); //绘图抗锯齿


    if (m_button_status == true)
    {
        painter.setBrush(m_backcolor_off); // 按钮设置颜色
    }
    else
    {
        painter.setBrush(m_backcolor_on); // 按钮设置颜色
    }
    QRect rect (0, 0, m_circle_width, m_circle_width); // 创建矩形
    int startX = rect.left() + rect.width() / 2;
    int startY = rect.top();

    // 画开关按钮
    path.moveTo(startX,startY);
    path.arcTo(QRect(rect.left(), rect.top(), rect.width(), rect.height()),90,180);
    path.lineTo((rect.left() + m_move_distance ), rect.bottom() + 1); // the graph not connet , neet 1 pixcel
    path.arcTo(QRect((startX + m_move_distance),rect.top(),rect.width(),rect.height()),270,180);
    path.lineTo(startX,startY);
    painter.drawPath(path);

    // 画按钮中的圆圈
    painter.setBrush(m_circle_color);
    painter.drawEllipse(m_button_pos ,0,m_circle_width,m_circle_width);
}

void SliderButton::slot_update()
{
    if (m_button_status == true)
    {
        m_button_pos += 1;
        if (m_button_pos == m_move_distance + m_circle_width / 2)
        {
            m_timer->stop();
        }
    }
    else if(m_button_status == false)
    {
        m_button_pos -= 1;
        if (m_button_pos == 0)
        {
            m_timer->stop();
        }
    }
    update(); // 调用paintEvent画图
}
